import os
from datetime import datetime,timedelta
import tornado.ioloop
import tornado.web
from tornado.options import define,options,parse_command_line

import pymysql
# get  post
define('port',default=8080,type=int)

class MainHandler(tornado.web.RequestHandler):
    def get(self,*args,**kwargs):
        # name= self.get_argument('name',default='kangbazi',strip=True)
        # name= self.get_query_argument('name',default='kangbazi',strip=True)
        name= self.get_query_arguments('name',strip=True)
        #name= self.get('name',strip=True)
        self.write("hello %s" % name)
    def post(self):
        #name = self.get_body_argument('name',default='haha',strip=True)
        name = self.get_body_arguments('name',strip=True)
        self.write('Hello,%s' % name)


class ResHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        self.write('<h1>6月穿棉袄打一城市</h1>')
        # self.set_status(500)
        # self.set_cookie('token','123321',expires_days=1)
        # out_time = datetime.now()+timedelta(days=1)
        # self.set_cookie('token666', '123321888', expires=out_time)
        #self.clear_cookie('token')
        # self.clear_all_cookies()
        self.redirect('/hello')
    def post(self):
        pass

class RoutesHandler(tornado.web.RequestHandler):
    def get(self,year,month,day):
        self.write('日期:%s年%s月%s日'%(day,month,year))

class Routes1Handler(tornado.web.RequestHandler):
    def get(self,year,month,day):
        self.write('日期:%s年%s月%s日'%(year,month,day))
    def post(self,year,month,day):
        self.write('处理post请求，要添加数据')
    def put(self,year,month,day):
        self.write('处理post请求，告诉数据修改为2')
    def delete(self,year,month,day):
        self.write('处理post请求，要删除数据')
    def patch(self,year,month,day):
        self.write('处理post请求，告诉元数据加1')

class EntryHandler(tornado.web.RequestHandler):
     def initialize(self):
        self.conn = pymysql.Connect(host='127.0.0.1',user='root',password='123456',database='tornado1',port=3306)
        self.cursor = self.conn.cursor()
        self.write('我就好比是魔术方法 init')

     def prepare(self):
         self.write('我在get方法之前调用')
     def get(self):
        self.write('我用来查询数据')
        sql = 'select * from stu'
        self.cursor.execute(sql)
        data = self.cursor.fetchall()
        print(data)
     def on_finish(self):
        self.conn.close()
class IndexHandler(tornado.web.RequestHandler):
    #flask是 render_template()
    def get(self):
        self.render('index.html')
def make_app():
    return tornado.web.Application(handlers=[
        (r'/',IndexHandler),
        (r'/hello',MainHandler),
        (r'/res',ResHandler),
        (r'/routes/(\d{4})/(\d{2})/(\d{2})/',RoutesHandler),
        (r'/routes1/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/',Routes1Handler),
        (r'/entry_point/',EntryHandler),

    ],autoreload=True,debug=True,template_path=os.path.join(os.path.dirname(__file__),'templates'))

if __name__ == "__main__":
    #解析命令行
    parse_command_line()
    #调用make_app()
    app = make_app()
    #监听端口
    app.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
